<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>采购指令明细</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		.table td {
			vertical-align: middle;
		}
		.footer {
			position:fixed; bottom:0; background:#ABABAB; width:100%; height:35px; line-height:32px; z-index:9999; opacity:1;
			filter:alpha(opacity=60); _bottom:auto; _width:100%; _position:absolute;text-align: right;
			_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-
			(parseInt(this.currentStyle.marginTop, 10)||0)-(parseInt(this.currentStyle.marginBottom, 10)||0)));
	    }
	</style>
	<script type="text/javascript">
		$(document).ready(function() {
			var nodeLen = $(".jbox-button-panel", top.document).length;
			$(".jbox-button-panel", top.document).eq(nodeLen-1).remove();
		});
        //检测商品编号
        function checkProductCode(){
        	var obj = $("#code");
        	if($("#code").val()==""){
				showJBoxMess("请输入商品编号或名称.","error");
				return;
			}
			if($(obj).val()!=""){
				//异步保存活动
				$.ajax({
					type: 'post',
					url: '${ctx}/ivm/inventory/checkProductCode',
					data: 'merchantId=${purchase.merchant.id}&code='+$(obj).val(),
					dataType: 'json',
					success: function(data){
						if(data.status==200){
							var product = data.result;
							var isExists = false;
				        	$("input[id='productCode']").each(function(){
				        		if($(this).val()==product.code){
				        			showJBoxMess("商品"+$(obj).val()+"已存在，请重新输入！","error");
				        			isExists=true;
				        			return false;
				        		}
				        	});
				        	if(isExists) return;
							createHmPurchaseDetail();
						}else{
							showJBoxMess(data.message,"error");
						}
					},
					failure: function(){
						showJBoxMess("操作失败","error");
					}				
				});
			}
		}
		function createHmPurchaseDetail(){
			//异步手动生成商品采购清单
			$.ajax({
				type: 'get',
				url: '${ctx}/ivm/purchase/createHmPurchase',
				data: 'merchantId=${purchase.merchant.id}&code='+$("#code").val(),
				dataType: 'json',
				success: function(data){
					if(data.status==200){
						//获取商品最大的序号
						var num = $(".detail").length;
						var detail = data.result;
						var html = '<tr class="editTr">';
						html += '<td class="detail">'+(num+1)+'</td>'
						html += '<td>';
						html += '<input type="hidden" id="detailId" name="detailId" value="'+num+'"/>';
						html += '<input type="hidden" id="type" name="type" value="2" />';
						html += '<input type="hidden" id="orderId" name="orderId" value="" />';
						html += '<input type="hidden" id="productId" name="productId" value="'+detail.product.id+'" />';
						html += '<input type="hidden" id="productCode" value="'+detail.product.code+'" />';
						html += detail.product.code;
						html += '</td>';
						html += '<td>'+detail.product.name+'</td>';
						if(detail.product.purchaseDesc==undefined){
							html += '<td></td>';
						}else{
							html += '<td>'+detail.product.purchaseDesc+'</td>';
						}
						html += '<td>'+detail.product.assisTitle+'</td>';
						html += '<td>';
						html += '<input name="purchaseNumber" value="1" htmlEscape="false" maxlength="20" class="required digits" onchange="updateNum(this, 0)" style="text-align: center; width: 80px; height: 100%; padding: 0px; margin:0px;"/>';
						html += '</td>';
						html += '<td>'+detail.product.specUnit.name+'</td>';
						html += '<td>手动</td>';
						html += '<td>';
						html += '<a href="javascript:" onclick="removeDetail(this)">删除</a>';
						html += '</td>';
						html += '</tr>';
						$(".detailBody").append(html);
						$("#code").val("");
					}else{
						showJBoxMess(data.message,"error");
					}
				},
				failure: function(){
					showJBoxMess("操作失败","error");
				}				
			});
		}
		//删除手动添加的商品
      	function removeDetail(obj){
      		top.$.jBox.defaults.top="27%";
      		top.$.jBox.confirm("确认删除该清单？",'系统提示',function(v,h,f){
				if(v=='ok'){
					$(obj).parent().parent().remove();
				}
			},{buttonsFocus:1});
			top.$('.jbox-body .jbox-icon').css('top','55px');
      	}
		//生成采购指令
		function createPurchase(){
			if($(".detail").length==0){
				showJBoxMess("请先添加采购商品.","error");
				return;
			}
			if($("#memberId").val()==""){
				showJBoxMess("请选择采购员.","error");
				return;
			}
			if($("#purchaseSupplierId").val()==""){
				showJBoxMess("请选择采购供应商.","error");
				return;
			}
			var detailList = "";
			var isOK = true;
			$("input[name='detailId']").each(function(index){
				var detailTr = $(this).parent().parent();
				var type = detailTr.find("input[name='type']").val();
				var orderId = detailTr.find("input[name='orderId']").val();
				var productId = detailTr.find("input[name='productId']").val();
				var purchaseNumber = detailTr.find("input[name='purchaseNumber']").val();
				var item = type+":"+orderId+":"+productId+":"+purchaseNumber;
				if(purchaseNumber=="0"){
					showJBoxMess("采购数量不能为0","error");
					isOK = false;
					return false;
				}
				detailList+=","+item;
			});
			if(!isOK) return;
			if(detailList!="") detailList = detailList.substring(1);
			//开始生成采购指令
			loading("正在提交，请稍等...");
			var param = "memberId="+$("#memberId").val()+"&supplierId="+$("#purchaseSupplierId").val()+"&detailList="+detailList;
			$.ajax({
				type: 'post',
				url: '${ctx}/ivm/purchase/createPurchase',
				data: param,
				dataType: 'json',
				success: function(data){
					if(data.status==200){
						showJBoxMess(data.message.split(":")[0],"success");
						$("#inputForm").attr("action","${ctx}/ivm/purchase/showPurchaseDetail?id="+data.message.split(":")[1]);
						setTimeout(function (){
							parent.mainFrame.location.reload();
							$("#inputForm").submit();
						},2000);
					}else{
						showJBoxMess(data.message,"error");
					}
				},
				failure: function(){
					showJBoxMess("操作失败","error");
				}
			});
		}
	</script>
</head>
<body>
<form:form id="inputForm" action="${ctx}/ivm/purchase/showPurchaseDetail?id=${purchase.id }" method="post">
	<div style="margin:10px;margin-bottom: 50px;">
		<table id="contentTable" class="table table-striped table-bordered table-condensed">
			<thead>
				<tr>
					<th>序号</th>
					<th>商品编号</th>
					<th>商品名称</th>
					<th>规格</th>
					<th>采购说明</th>
					<th>计划采购量</th>
					<th>采购单位</th>
					<th>来源</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody class="detailBody">
			
			</tbody>
		</table>
		<!-- 派送员信息 -->
		<ul class="wl-tab-nav">
			<li><span>分配采购员</span></li>
		</ul>
		<div class="delivery_info">
			<label>采购员:</label>
			<select id="memberId" class="required input-small">
				<option value="">请选择</option>
				<c:forEach  items="${fns:getDeliveryMemberList(2)}" var="item">
					<option value="${item.id }">${item.name }</option>
				</c:forEach>
			</select>
			<label>采购供应商：</label>
			<select id="purchaseSupplierId" class="required" style="width: 150px;">
				<option value="">请选择</option>
				<c:forEach items="${supplierList}" var="supplier">
				<option value="${supplier.id }">${supplier.name }</option>
				</c:forEach>
			</select>
			<input class="btn btn-primary" type="button" onclick="createPurchase()" value="生成采购指令"/>
		</div>
	</div>
</form:form>
<!--endprint-->
<div class="footer" id="printDiv">
	<input type="text" id="code" name="code" htmlEscape="false" maxlength="200" class="required input-small" style="margin-bottom: 0px; width: 180px;" placeholder="请输入商品编号或名称"/>
	<input class="btn btn-primary" type="button" onclick="checkProductCode()" value="添加采购商品" style="margin-right:10px;"/>
</div>
</body>
</html>